import java.util.*;

public class SolutionLCP57 {
    public int getMaximumNumber(int[][] moles) {
        int ans=0;
        Map<List<Integer>, Set<Integer>> map=new HashMap<>();
        map.put(Arrays.asList(0,1),new HashSet<>(Arrays.asList(0,1,3)));
        map.put(Arrays.asList(0,2),new HashSet<>(Arrays.asList(0,1,2,3,4,6)));
        map.put(Arrays.asList(0,3),new HashSet<>(Arrays.asList(0,1,2,3,4,5,6,7)));
        map.put(Arrays.asList(1,1),new HashSet<>(Arrays.asList(0,1,2,4)));
        map.put(Arrays.asList(1,2),new HashSet<>(Arrays.asList(0,1,2,3,4,5,7)));
        map.put(Arrays.asList(2,1),new HashSet<>(Arrays.asList(1,2,5)));
        map.put(Arrays.asList(2,2),new HashSet<>(Arrays.asList(0,1,2,4,5,8)));
        map.put(Arrays.asList(2,3),new HashSet<>(Arrays.asList(0,1,2,3,4,5,7,8)));
        map.put(Arrays.asList(3,1),new HashSet<>(Arrays.asList(0,3,4,6)));
        map.put(Arrays.asList(3,2),new HashSet<>(Arrays.asList(0,1,3,4,5,6,7)));
        map.put(Arrays.asList(4,1),new HashSet<>(Arrays.asList(1,3,4,5,7)));
        map.put(Arrays.asList(5,1),new HashSet<>(Arrays.asList(2,4,5,8)));
        map.put(Arrays.asList(5,2),new HashSet<>(Arrays.asList(1,2,3,4,5,7,8)));
        map.put(Arrays.asList(6,1),new HashSet<>(Arrays.asList(3,6,7)));
        map.put(Arrays.asList(6,2),new HashSet<>(Arrays.asList(0,3,4,6,7,8)));
        map.put(Arrays.asList(6,3),new HashSet<>(Arrays.asList(0,1,3,4,5,6,7,8)));
        map.put(Arrays.asList(7,1),new HashSet<>(Arrays.asList(4,6,7,8)));
        map.put(Arrays.asList(7,2),new HashSet<>(Arrays.asList(1,3,4,5,6,7,8)));
        map.put(Arrays.asList(8,1),new HashSet<>(Arrays.asList(5,7,8)));
        map.put(Arrays.asList(8,2),new HashSet<>(Arrays.asList(2,4,5,6,7,8)));
        map.put(Arrays.asList(8,3),new HashSet<>(Arrays.asList(1,2,3,4,5,6,7,8)));
        int[] ceiling=new int[]{3,2,3,2,1,2,3,2,3};
        TreeMap<Integer, List<Integer>> treeMap=new TreeMap<>();
        for (int[] mole : moles) {
            treeMap.putIfAbsent(mole[0],new ArrayList<>());
            treeMap.get(mole[0]).add(mole[1]*3+mole[2]);
        }
        int[][] f=new int[treeMap.size()][9];
        List<Integer> list=new ArrayList<>(treeMap.keySet());
        for (int i=0;i<list.size();i++){
            if (list.get(i)==0){
                for (int k=0;k<9;k++){
                    f[i][k]=0;
                }
                if (treeMap.get(0).contains(4)){
                    f[i][4]=1;
                }
            }else if (list.get(i)==1){
                for (int k=1;k<=7;k+=2){
                    if (treeMap.containsKey(0)){
                        f[i][k]=f[0][4];
                    }
                    if (treeMap.get(1).contains(k)){
                        f[i][k]++;
                    }
                }
                f[i][4]=f[0][4];
                if (treeMap.get(1).contains(4)){
                    f[i][4]++;
                }
            }else{
                for (int k=0;k<9;k++){
                    for (int p=i-1;p>=0;p--){
                        if (list.get(i)-list.get(p)>ceiling[k]){
                            for (int l=0;l<9;l++){
                                f[i][k]=Math.max(f[i][k],f[p][l]);
                            }
                            break;
                        }else{
                            for (Integer l : map.get(Arrays.asList(k, list.get(i) - list.get(p)))) {
                                f[i][k]=Math.max(f[i][k],f[p][l]);
                            }
                        }
                    }
                    if (treeMap.get(list.get(i)).contains(k)){
                        f[i][k]++;
                    }
                }
            }
            for (int k=0;k<9;k++){
                ans=Math.max(ans,f[i][k]);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new SolutionLCP57().getMaximumNumber(new int[][]{{1,1,0},{2,0,1},{4,2,2}}));
    }
}
